package com.cong.modules.job.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cong.common.util.StringUtils;
import com.cong.common.util.UUIDUtil;
import com.cong.common.util.text.Convert;
import com.cong.modules.job.mapper.SysJobLogMapper;
import com.cong.modules.job.model.SysJobLog;
import com.cong.modules.job.service.SysJobLogService;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class SysJobLogServiceImpl extends ServiceImpl<SysJobLogMapper, SysJobLog> implements
        SysJobLogService {

    @Resource
    private SysJobLogMapper sysJobLogMapper;

    @Override
    public SysJobLog getSysJobLogById(Long jobLogId) {
        return sysJobLogMapper.selectById(jobLogId);
    }

    @Override
    public void addJobLog(SysJobLog jobLog) {
        jobLog.setJobLogId(Long.valueOf(UUIDUtil.getUniqueIdByUUId()));
        jobLog.setCreateTime(new Date());
        sysJobLogMapper.insert(jobLog);
    }

    @Override
    public List<SysJobLog> listSysLogJobs(SysJobLog sysJobLog, String[] dateRanges) {
        LambdaQueryWrapper<SysJobLog> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(sysJobLog.getJobName())) {
            queryWrapper.like(SysJobLog::getJobName, sysJobLog.getJobName());
        }
        if (sysJobLog.getStatus() != null) {
            queryWrapper.eq(SysJobLog::getStatus, sysJobLog.getStatus());
        }
        if (dateRanges.length > 0) {
            queryWrapper.between(SysJobLog::getCreateTime, dateRanges[0].trim(), dateRanges[1].trim());
        }
        queryWrapper.orderByDesc(SysJobLog::getCreateTime);
        return sysJobLogMapper.selectList(queryWrapper);
    }

    @Override
    public void deleteJobLogByIds(String jobLogIdStr) {
        Long[] jobLogIds = Convert.toLongArray(",", jobLogIdStr);
        LambdaQueryWrapper<SysJobLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SysJobLog::getJobLogId, Arrays.asList(jobLogIds));
        sysJobLogMapper.delete(queryWrapper);
    }

}
